package com.example.demo.controller;

import com.example.demo.pojo.TokenData;
import com.example.demo.pojo.User;
import com.example.demo.service.UserService;
import com.example.demo.utils.Result;
import com.example.demo.utils.ResultFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * 借阅卡 - 读者模块
 */
@RestController
@CrossOrigin
@RequestMapping("/reader")
public class ReaderController {

    @Autowired
    private UserService userService;

    private Logger logger = LoggerFactory.getLogger(ReaderController.class);

    /**
     * 暂停借阅证
     * private int state; //1-正常,2暂停,3-挂失,4-注销
     */
    @PatchMapping("/stopBorrowCard/{reader_id}")
    public Result stopBorrowCard(@PathVariable int reader_id, HttpSession session){

        TokenData currentUser =(TokenData) session.getAttribute("currentUser");
        Result result = null;
        if(currentUser.getUser_state() == 1){  //判断是否为管理员
            result=this.userService.updateUserStateByReaderId(reader_id,2);
        }else{
           result = ResultFactory.failureWithStatus(999,"抱歉！你不是管理员，没有此权限");
        }
        return result;
    }


    /**
     * 暂停借阅证
     * private int state; //1-正常,2暂停,3-挂失,4-注销
     */
    @PatchMapping("/cancelBorrowCard/{reader_id}")
    public Result cancelBorrowCard(@PathVariable int reader_id, HttpSession session){

        TokenData currentUser =(TokenData) session.getAttribute("currentUser");
        Result result = null;
        if(currentUser.getUser_state() == 1){  //判断是否为管理员
            result=this.userService.updateUserStateByReaderId(reader_id,4);
        }else{
            result = ResultFactory.failureWithStatus(999,"抱歉！你不是管理员，没有此权限");
        }
        return result;
    }

    /**
     * 挂失借阅证
     * private int state; //1-正常,2暂停,3-挂失,4-注销
     */
    @PatchMapping("/reportLossOfBorrowCard/{reader_id}")
    public Result reportLossOfBorrowCard(@PathVariable int reader_id, HttpSession session){

        TokenData currentUser =(TokenData) session.getAttribute("currentUser");
        Result result = null;
        if(currentUser.getUser_state() == 1){  //判断是否为管理员
            result=this.userService.updateUserStateByReaderId(reader_id,3);
        }else{
            result = ResultFactory.failureWithStatus(999,"抱歉！你不是管理员，没有此权限");
        }
        return result;
    }

    /**
     * 根据id查询借阅证信息 (√)
     */
    @GetMapping("/findReaderInfoByReaderId/{reader_id}")
    public Result findReaderInfoByReaderId(@PathVariable int reader_id){
        return this.userService.selReaderInfoById(reader_id);
    }
    /**
     * 修改借阅证信息
     */
    @PatchMapping("/updateReader")
    public Result updateReaderInfoByInfo(User user){

        Result result = this.userService.updateReaderInfo(user);
        if(result==null){
            result = ResultFactory.failureWithStatus(999,"借阅卡信息修改失败");
        }
        return result;
    }

    /***
     * 个人信息展示
     */
    @GetMapping("/selUserByLogin")
    public Result selUserByLogin(HttpSession session){

        TokenData tokenData = (TokenData) session.getAttribute("currentUser");
        return this.userService.selReaderInfoById(tokenData.getReader_id());

    }

}
